package org.mao.forumtest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ReadImage {

	public static void main(String[] arg) {
		try {
			ReadImage image = new ReadImage();
			image.read("http://38.103.161.184/bbs/thread-9966728-1-1.html"); // 千万不要在浏览器里直接打开这个链接，否则后果自负
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void read(String url) throws Exception {
		Document doc = Jsoup.connect(url).get();
		//Document doc = Jsoup.parse(new File("F:\\contract\\download\\page.txt"),"UTF-8");
		
		File file = new File("D:\\readtimeout.txt");
		FileWriter fw = new FileWriter(file);
		fw.write(doc.html());
		fw.close();
		
//		Elements forms = doc.getElementsByTag("form");
//		if (forms.size() > 0) {
//			Element form = forms.get(0);
//			Elements tables = form.select(">div>table");
//			if (tables.size() > 0) {
//				Element table = tables.get(0);
//				Elements divItems = table.select("div[class='t_msgfont']");
//				if (divItems.size() > 0) {
//					Element divItem = divItems.get(0);
//					String text = divItem.text();
//					text = text.replace("【", "\r\n【");
//					text = text.trim();
//					System.out.println(text);
//					Elements images = divItem.select("img");
//					for (Element image : images) {
//						String imageUrl = image.attr("abs:src");
//						System.out.println(imageUrl);
//						readAttarch(doc);
//					}
//				}
//			}
//
//		}
	}
	
	public void readAttarch(Document doc) {
		try {
			Elements forms = doc.getElementsByTag("form");
			if (forms.size() > 0) {
				Element form = forms.get(0);
				Elements tables = form.select(">div>table");
				if (tables.size() > 0) {
					Element table = tables.get(0);
					//<dl class="t_attachlist"> 
					Elements linkItems = table.select("dl[class='t_attachlist']>dt>a");
					for(Element link: linkItems){
						if(link.text().endsWith("torrent")){
							String torrentUrl = link.attr("abs:href").trim();
							System.out.println(torrentUrl);
							System.out.println(link.text().trim());
							this.readTorrentUrl(torrentUrl);
						}
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private File readImageFromUrl(String urlStr) throws Exception {
		InputStream is = null;
		FileOutputStream out = null;
		File file = null;
		try {
			URL url = new URL(urlStr);
			URLConnection conn = url.openConnection();
			conn.setConnectTimeout(1000);
			conn.setReadTimeout(1000);

			byte[] buffer = new byte[0];
			int count = 0;
			while (buffer.length == 0 || count < 10000) {
				count++;
				is = conn.getInputStream();
				buffer = new byte[is.available()];
			}

			String fileName = urlStr.substring(urlStr.lastIndexOf("/") + 1, urlStr.length());
			String contractDownload = "D:";
			file = new File(contractDownload + "/" + fileName);
			if (!file.exists()) {
				file.createNewFile();
			}
			out = new FileOutputStream(file);

			int c;
			while ((c = is.read(buffer)) != -1) {
				// System.out.println("c:" + c);
				out.write(buffer, 0, c);
				// Thread.sleep(100);
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (is != null) {
				is.close();
			}
			if (out != null) {
				out.close();
			}
		}
		return file;
	}
	
	private File readTorrentUrl(String torrentUrl) throws Exception {
		InputStream is = null;
		FileOutputStream out = null;
		File file = null;
		try {
			URL url = new URL(torrentUrl);
			URLConnection conn = url.openConnection();
			conn.setConnectTimeout(1000);
			conn.setReadTimeout(1000);

			byte[] buffer = new byte[0];
			int count = 0;
			while (buffer.length == 0 || count < 10000) {
				count++;
				is = conn.getInputStream();
				buffer = new byte[is.available()];
			}

			String fileName = "a.torrent";
			String contractDownload = "D:";
			file = new File(contractDownload + "/" + fileName);
			if (!file.exists()) {
				file.createNewFile();
			}
			out = new FileOutputStream(file);

			int c;
			while ((c = is.read(buffer)) != -1) {
				// System.out.println("c:" + c);
				out.write(buffer, 0, c);
				// Thread.sleep(100);
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (is != null) {
				is.close();
			}
			if (out != null) {
				out.close();
			}
		}
		return file;
	}
	
	//http://38.103.161.184/bbs/attachment.php?aid=2925612
}
